//
//  LPZWYMYHeaderTableViewCell.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2018/6/4.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LPZWYMYHeaderTableViewCell.h"
#import <SDWebImage/UIButton+WebCache.h>

#import "LPHeadSecondFuncationView.h"
@interface LPZWYMYHeaderTableViewCell ()

/**  背景图  */
@property (nonatomic , strong) UIImageView * backImageView;
/**    */
@property (strong, nonatomic) UIView  *headView;

/**  头像  */
@property (nonatomic , strong) UIButton * headImageView;
//姓名
@property (nonatomic , strong) UILabel * nameLabel;

/**  第二部分  */
@property (strong, nonatomic) UIView  *secondView;

/**  退出按钮  */
@property (strong, nonatomic) UILabel  *backButton;



@end
@implementation LPZWYMYHeaderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}


#pragma mark - 界面
- (void)layoutUI{
    
    [self addSubview:self.backImageView];
    [self.backImageView addSubview:self.headView];
    [self.headView addSubview:self.headImageView];
    [self.backImageView addSubview:self.nameLabel];
    [self.backImageView addSubview:self.backButton];
    
    
    TenGrayShadowView *ten = [TenGrayShadowView new];
    [self addSubview:ten];
    ten.sd_layout.leftEqualToView(self)
    .rightEqualToView(self)
    .bottomEqualToView(self)
    .heightIs(5 * [UIScreen zj_screenHScale]);
    
    self.backImageView.sd_layout
    .topEqualToView(self)
    .leftEqualToView(self)
    .heightIs(120 * [UIScreen zj_screenHScale])
    .rightEqualToView(self);
    
    
    self.headView.sd_layout
    .leftSpaceToView(self.backImageView, 20 * [UIScreen zj_screenWScale])
    .centerYEqualToView(self.backImageView)
    .heightIs(90 * [UIScreen zj_screenHScale])
    .widthIs(90 * [UIScreen zj_screenHScale]);
    
    self.headImageView.sd_layout
    .centerYEqualToView(self.headView)
    .centerXEqualToView(self.headView)
    .heightIs(82 * [UIScreen zj_screenHScale])
    .widthIs(82 * [UIScreen zj_screenHScale]);
    
    
    self.nameLabel.sd_layout
    .leftSpaceToView(self.headView, 15 * [UIScreen zj_screenWScale])
    .heightIs(30 * [UIScreen zj_screenHScale])
    .widthIs(100)
    .centerYEqualToView(self.backImageView);
    
    self.backButton.sd_layout
    .centerYEqualToView(self.backImageView)
    .rightSpaceToView(self.backImageView, 50 * [UIScreen zj_screenWScale])
    .heightIs(30 * [UIScreen zj_screenHScale])
    .widthIs(60 * [UIScreen zj_screenWScale]);
    
    
    
    
    
    [self addSubview:self.secondView];
    self.secondView.sd_layout
    .bottomSpaceToView(self, 5 * [UIScreen zj_screenHScale])
    .heightIs(115 * [UIScreen zj_screenHScale])
    .leftEqualToView(self)
    .rightEqualToView(self);
    
   
    
    [self funcation];
}

- (void)setModel:(LPZWYMyDetailModel *)model{
    _model = model;
    
    [self funcation];
}

#pragma mark - 私有方法
- (void)funcation{
    self.nameLabel.text = YBUSERINFOMODEL.name;
    [self.headImageView sd_setImageWithURL:CREATEURL(YBUSERINFOMODEL.photothumbpath) forState:UIControlStateNormal placeholderImage:PlaceholderHeaderImage];
    CGFloat width = [UIScreen zj_screenWith] / 3;
    NSArray *colors = @[@"ff7638", @"319aff", @"d74acc"];
    NSArray *titles = @[@"已获学时", @"结业专题", @"已学课程"];
     NSArray *numbers ;
    if (self.model) {
        numbers = @[[NSString stringWithFormat:@"%.1f",[self.model.allscore floatValue]] ,self.model.certificatecount,self.model.finishedcount ];
    }else{
        numbers = @[@"0", @"0", @"0"];
    }
   
    for (UIView *subView in self.secondView.subviews) {
        [subView removeFromSuperview];
    }
    for (int i = 0; i < colors.count; i ++) {
        
        LPHeadSecondFuncationView *funcation = [[LPHeadSecondFuncationView alloc] initWithFrame:CGRectMake(width * i, 22 * [UIScreen zj_screenHScale], width - 0.5, 60 * [UIScreen zj_screenHScale]) title:titles[i] number:numbers[i] color:colors[i]];
        funcation.userInteractionEnabled = YES;
        funcation.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap_funcationView:)];
        [funcation addGestureRecognizer:tap];
        [self.secondView addSubview:funcation];
        if (i != 2) {
            UIView *luxian = [[UIView alloc] initWithFrame:CGRectMake((width - 0.5) * (i + 1), 22 * [UIScreen zj_screenHScale], 1, 60 * [UIScreen zj_screenHScale])];
            luxian.backgroundColor = [UIColor themeGrayColor];
            [self.secondView addSubview:luxian];
        }
    }
}

- (void)tap_funcationView:(UITapGestureRecognizer *)tap{
    if (self.delegate) {
        [self.delegate LPZWYMYHeaderTableViewCell_clickFuncationViewWithTag:tap.view.tag];
    }
}

/**  退出按钮的点击事件  */
- (void)tapButton{
    if (self.delegate) {
        [self.delegate LPZWYMYHeaderTableViewCell_clickHeadImage];
    }
}

-(void)option_headImageButton:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(LPZWYMYHeaderTableViewCell_clickIntoSettingView)]) {
        [self.delegate LPZWYMYHeaderTableViewCell_clickIntoSettingView];
    }
}

#pragma mark - getter

- (UIImageView *)backImageView{
    if (_backImageView == nil) {
        _backImageView = [UIImageView new];
        _backImageView.userInteractionEnabled = YES;
        _backImageView.image = [UIImage imageNamed:@"my_bg"];
        
    }
    return _backImageView;
}

- (UIView *)headView
{
    if (_headView == nil) {
        _headView = [UIView new];
        _headView.layer.cornerRadius = 45 * [UIScreen zj_screenHScale];
        _headView.backgroundColor = [UIColor whiteColor];
    }
    return _headView;
}

- (UIButton *)headImageView{
    if (_headImageView == nil) {
        _headImageView = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"" textColor:[UIColor whiteColor] imageName:@"" font:15];
        _headImageView.layer.cornerRadius = 41 * [UIScreen zj_screenHScale];
        [_headImageView addTarget:self action:@selector(option_headImageButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _headImageView;
}

- (UILabel *)nameLabel{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = LYPBOLDFONT(22);
        
    }
    return _nameLabel;
}

- (UILabel *)backButton{
    if (_backButton == nil) {
        _backButton = [UILabel new];
        _backButton.font = LYPFONT(17);
        _backButton.textColor = [UIColor whiteColor];
        _backButton.text = @"退出";
        _backButton.textAlignment = NSTextAlignmentCenter;
        _backButton.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapButton)];
        [_backButton addGestureRecognizer:tap];
        
    }
    return _backButton;
}



- (UIView *)secondView{
    if (_secondView == nil) {
        _secondView = [UIView new];
        _secondView.backgroundColor = [UIColor whiteColor];
    }
    return _secondView;
}

@end
